package com.zeldu.uaa.controller;


import com.zeldu.common.exception.CustomException;
import com.zeldu.common.exception.ExceptionCast;
import com.zeldu.common.model.response.CommonCode;
import com.zeldu.common.model.response.QueryResponseResult;
import com.zeldu.common.model.response.QueryResult;
import com.zeldu.common.model.response.ResponseResult;
import com.zeldu.common.util.PhoneUtil;
import com.zeldu.uaa.DTO.AuthToken;
import com.zeldu.uaa.DTO.LoginRequestDTO;
import com.zeldu.uaa.DTO.LoginResult;
import com.zeldu.uaa.entity.Student;
import com.zeldu.uaa.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController

public class AuthController {
    @Autowired
    AuthService authService;
    @Value("${auth.clientId}")
    String clientId;
    @Value("${auth.clientSecret}")
    String clientSecret;
@PostMapping("/userlogin")
    public LoginResult login( LoginRequestDTO logInRequestDTO) throws CustomException{
  //  throw new CustomException(CommonCode.AUTH_CREDENTIAL_ERROR);

    //校验账号是否输入
    if(logInRequestDTO == null || StringUtils.isEmpty(logInRequestDTO.getUsername())){
       // ExceptionCast.cast(CommonCode.AUTH_USERNAME_NONE);
        throw new CustomException(CommonCode.AUTH_ACCOUNT_NOTEXISTS);
    }
    //org.springframework.security.authentication.InternalAuthenticationServiceException: UserDetailsService returned null, which is an interface contract violation
//校验密码是否输入
    if(StringUtils.isEmpty(logInRequestDTO.getPassword())){
        ExceptionCast.cast(CommonCode.AUTH_PASSWORD_NONE);
    }


     AuthToken authToken = authService.login(logInRequestDTO, clientId, clientSecret);

    return new LoginResult(CommonCode.SUCCESS,authToken.getJwt_token());
    }




}
